package com.atguigu.democustomer.controller;

import com.atguigu.democustomer.bean.Customer;
import com.atguigu.democustomer.service.CustomerService;
import com.atguigu.democustomer.service.impl.CustomerSerivceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
public class CustomerController {

    @Autowired
    CustomerService customerService;


    @RequestMapping(value = "customer")
    public String getCustomerByName(@RequestParam("name") String na){
        System.out.println( "Customer name:"+na);
        return "success";
    }

    //通常用于根据主键查找
    @RequestMapping("customerById/{id}")
    public Customer getCustomerById(@PathVariable("id") String id){
//        System.out.println( "Customer Id:"+id);

        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);

        Customer customer = customerService.getOne(queryWrapper);


        return customer;
    }

    //接收post请求，可以传进来一个表单 / 对象
    @PostMapping("customer")
    public void saveCustomer(@RequestBody Customer customer){
//        System.out.println("保存 customer:"+customer);

//        customerService.saveCustomer(customer);



//        customerService.save(customer);
//        customerService.saveOrUpdate(customer);
        customerService.saveCustomer(customer);
    }

}
